﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RNA_Libraries
{
  public class MutationGeneration
  {
    static readonly Random Rnd = new Random(DateTime.Now.Millisecond);
    
    public static Sequence Mutate(Sequence original, double solidity)
    {
      int l = original.Length;
      int cur = 0;
      int solidityParameter = (int) (solidity*l);
      int nextMut = Rnd.Next(solidityParameter);
      StringBuilder res = new StringBuilder(l);
      while (cur < l)
      {
        if (cur < nextMut)
        {
          res.Append(original[cur++]);
        }
        else
        {
          if (Rnd.Next(1) == 1) res.Append(original[cur]);
          res.Append(SequenceGenerator.RandomSymbol);
           
          nextMut += Rnd.Next(solidityParameter);
          cur++;
        }
      }
      return new Sequence(res.ToString());
    }

    public static List<IMutation> GenerateMutations(Sequence original, double solidity)
    {
      var muts = new List<IMutation>();
      int solidityParameter = (int)(solidity * original.Length);
      int cur = 0;
      
      while (cur<solidityParameter)
      {
        cur++;
        IMutation m;
        switch ((MutationType)Rnd.Next(2))
        {
          case MutationType.Delete:
            m = new DeleteMutation(Rnd.Next(original.Length));
            break;
          case MutationType.Insert:
            m = new InsertMutation(Rnd.Next(original.Length), SequenceGenerator.RandomSymbol);
            break;
          case MutationType.Replace:
            m = new ReplaceMutation(Rnd.Next(original.Length), SequenceGenerator.RandomSymbol);
            break;
          default:
            m = new IdentMutation(Rnd.Next(original.Length));
            break;
        }
        muts.Add(m);
      }

      return muts;
    }
  }
}
